package com.ithong.controller;

import com.alibaba.fastjson.JSON;
import com.ithong.entity.User;
import com.ithong.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    @RequestMapping("/login")
    public String login(String loginName, String password, HttpSession session){
        //创建map集合保存信息
        Map<String, Object> map = new HashMap<String, Object>();
        //调用登录的方法
        User loginUser = userService.login(loginName, password);
        //判断对象是否为空
        if(loginUser!=null){
            //保存会话
            session.setAttribute("loginUser",loginUser);
            //无刷新
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        //将map集合的数据以json格式返回
        return JSON.toJSONString(map);
    }
}
